package com.kingbook.api.rest;

import com.kingbook.core.annotations.ContainProperties;
import com.kingbook.core.auth.LoginManager;
import com.kingbook.core.result.JSONPageResult;
import com.kingbook.core.result.JSONResult;
import com.kingbook.services.ImagesService;
import com.kingbook.services.WorkorderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static com.kingbook.core.auth.LoginManager.AUTH;

/**
 * @Author Bucky
 * @Date 2019-12-08 17:18:13
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("workorder")
public class WorkorderRest {

    @Autowired
    WorkorderService workorderService;
    @Autowired
    ImagesService imagesService;

    @PutMapping
    @ContainProperties({"workDate", "chargeType", "useType", "address", "plateNo", "driver", "tons"})
    public JSONResult save(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        param.put("mid", LoginManager.getMid(auth));
        log.info("---->{}", param);
        return workorderService.updateWorkorder(param);
    }

    @PostMapping("pages")
    @ContainProperties({"pageNo", "pageSize"})
    public JSONPageResult queryForPages(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        param.put("mid", LoginManager.getMid(auth));
        log.info("---->{}", param);
        int useType = MapUtils.getIntValue(param, "useType", 0);
        if (useType == 3) {
            param.put("allotType", 2);
            param.remove("useType");
        }
        return workorderService.queryForPages(param);
    }

    @PostMapping("query")
    public JSONResult query(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        param.put("mid", LoginManager.getMid(auth));
        log.info("---->{}", param);
        return workorderService.query(param);
    }

    @PostMapping("statistics")
    public JSONResult statistics(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        param.put("mid", LoginManager.getMid(auth));
        log.info("---->{}", param);
        return workorderService.statistics(param);
    }

    @DeleteMapping("{id}")
    public JSONResult remove(@PathVariable int id, @RequestHeader(name = AUTH) String auth) {
        int mid = LoginManager.getMid(auth);
        log.info("---->mid={} id={}", mid, id);
        return workorderService.remove(id, LoginManager.getMid(auth));
    }

    @GetMapping("{id}")
    public JSONResult find(@PathVariable int id, @RequestHeader(name = AUTH) String auth) {
        int mid = LoginManager.getMid(auth);
        log.info("---->mid={} id={}", mid, id);
        return workorderService.find(id, mid);
    }

    @PostMapping("confirm")
    @ContainProperties({"id", "fromOid"})
    public JSONResult confirm(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        param.put("mid", LoginManager.getMid(auth));
        log.info("---->{}", param);
        return workorderService.confirm(param);
    }

    @PostMapping("deny")
    @ContainProperties({"id"})
    public JSONResult deny(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        param.put("mid", LoginManager.getMid(auth));
        log.info("---->{}", param);
        return workorderService.deny(param);
    }

    @GetMapping("findAllotToPartner/{id}")
    public JSONResult findAllotToPartner(@PathVariable int id, @RequestHeader(name = AUTH) String auth) {
        int mid = LoginManager.getMid(auth);
        log.info("---->mid={} id={}", mid, id);
        return workorderService.findAllotToPartner(id, mid);
    }

    @PostMapping("queryAllot")
    public JSONResult queryAllot(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        int mid = LoginManager.getMid(auth);
        param.put("mid", mid);
        return workorderService.queryAllot(param);
    }

    @GetMapping("favorite/{id}")
    public JSONResult favorite(@PathVariable int id, @RequestHeader(name = AUTH) String auth) {
        int mid = LoginManager.getMid(auth);
        log.info("---->mid={} id={}", mid, id);
        return workorderService.doFavorite(id, mid);
    }


    @GetMapping("useStatistics")
    public JSONResult useStatistics(@RequestHeader(name = AUTH) String auth) {
        int mid = LoginManager.getMid(auth);
        log.info("---->mid={} ", mid );
        try {
            return workorderService.statisticsForUse(mid);
        } catch (Exception e) {
            log.error("---->统计使用量失败.");
            return JSONResult.failed("统计使用量失败");
        }
    }


    @PostMapping("queryForFinance")
    @ContainProperties({"isCustomer", "ownerId", "allotType", "financeId"})
    public JSONResult queryForFinance(@RequestBody Map<String, Object> param, @RequestHeader(name = AUTH) String auth) {
        param.put("mid", LoginManager.getMid(auth));
        log.info("---->{}", param);
        boolean isCustomer = MapUtils.getBoolean(param, "isCustomer");
        int allotType = MapUtils.getIntValue(param, "allotType");
        int ownerId = MapUtils.getIntValue(param, "ownerId");
        param.put(isCustomer ? "cid" : "pid", ownerId);
        if (allotType == 1)
            return workorderService.queryForReceipt(param);
        else if (!isCustomer) {
            return workorderService.queryForPay(param);
        }
        return JSONResult.failed("操作类型不支持");
    }

}
